//
//  ChooseItemsInCurveViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/10.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "ChooseItemsInCurveViewController.h"

@interface ChooseItemsInCurveViewController ()
{
    
}

@property (nonatomic,strong) UIView *jumpView;

@property (nonatomic,strong) NSArray *colorsArray;

@property (nonatomic,strong) NSArray *itemsArray;

@property (nonatomic,strong) NSMutableArray *typeArray;
@end

@implementation ChooseItemsInCurveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadArrays];
    [self creatBackButton];
    [self creatJumpView];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.5 animations:^{
        _jumpView.frame = CGRectMake(10, _viewFrame.origin.y + _viewFrame.size.height, _jumpView.sizeWidth, _jumpView.sizeHeight);
    }];
}

#pragma mark - 获取传入的数据

- (void)getSingleShopInfoByGrowth_fans_num:(BOOL)growth_fans_num
                        total_charging_num:(BOOL)total_charging_num
                         used_charging_num:(BOOL)used_charging_num
                       average_per_day_add:(BOOL)average_per_day_add
                             charging_rate:(BOOL)charging_rate
                                    dabiao:(BOOL)dabiao{
    
    _growth_fans_num = growth_fans_num;
    _total_charging_num = total_charging_num;
    _used_charging_num = used_charging_num;
    _average_per_day_add = average_per_day_add;
    _charging_rate = charging_rate;
    _dabiao = dabiao;
    
    _itemsArray = @[@"涨粉数",@"总设备",@"使用设备",@"单台日均",@"平均在线数",@"达标"];
    
    _typeArray = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:_growth_fans_num],
                  [NSNumber numberWithBool:_total_charging_num],
                  [NSNumber numberWithBool:_used_charging_num],
                  [NSNumber numberWithBool:_average_per_day_add],
                  [NSNumber numberWithBool:_charging_rate],
                  [NSNumber numberWithBool:_dabiao], nil];
    

}

- (void)getHistoryPersonInfoByCharging_average_per_day_add:(BOOL)charging_average_per_day_add
                                 store_average_per_day_add:(BOOL)store_average_per_day_add
                                                 store_num:(BOOL)store_num
                                              charging_num:(BOOL)charging_num
                                                 sub_count:(BOOL)sub_count
                                          use_charging_num:(BOOL)use_charging_num
                                              ok_store_num:(BOOL)ok_store_num
                                             charging_rate:(BOOL)charging_rate
                                                  wifi_num:(BOOL)wifi_num
                                            wifi_sub_count:(BOOL)wifi_sub_count
                                             wifi_used_num:(BOOL)wifi_used_num
                                                  mima_num:(BOOL)mima_num
                                            mima_sub_count:(BOOL)mima_sub_count
                                             mima_used_num:(BOOL)mima_used_num{
    
    _charging_average_per_day_add = charging_average_per_day_add;
    _store_average_per_day_add = store_average_per_day_add;
    _store_num = store_num;
    _charging_num = charging_num;
    _sub_count = sub_count;
    _use_charging_num = use_charging_num;
    _ok_store_num = ok_store_num;
    _charging_rate = charging_rate;
    _wifi_num = wifi_num;
    _wifi_sub_count = wifi_sub_count;
    _wifi_used_num = wifi_used_num;
    _mima_num = mima_num;
    _mima_sub_count = mima_sub_count;
    _mima_used_num = mima_used_num;
    
    
    _itemsArray = @[@"单台日均涨粉",@"单店日均涨粉",@"已铺门店",@"已铺设备",@"当日新增",@"使用设备",@"达标门店",@"设备使用率",@"WiFi设备数",@"WiFi设备涨粉数",@"WiFi设备使用数",@"密码设备",@"密码设备涨粉数",@"密码设备使用数"];
    
    _typeArray = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:_charging_average_per_day_add],
                  [NSNumber numberWithBool:_store_average_per_day_add],
                  [NSNumber numberWithBool:_store_num],
                  [NSNumber numberWithBool:_charging_num],
                  [NSNumber numberWithBool:_sub_count],
                  [NSNumber numberWithBool:_use_charging_num],
                  [NSNumber numberWithBool:_ok_store_num],
                  [NSNumber numberWithBool:_charging_rate],
                  [NSNumber numberWithBool:_wifi_num],
                  [NSNumber numberWithBool:_wifi_sub_count],
                  [NSNumber numberWithBool:_wifi_used_num],
                  [NSNumber numberWithBool:_mima_num],
                  [NSNumber numberWithBool:_mima_sub_count],
                  [NSNumber numberWithBool:_mima_used_num], nil];

}

- (void)getCommistionHistoryByHistory_ok_num:(BOOL)history_ok_num
                           history_store_num:(BOOL)history_store_num
                               history_money:(BOOL)history_money
                                today_ok_num:(BOOL)today_ok_num
                             today_store_num:(BOOL)today_store_num
                                 today_money:(BOOL)today_money
                                 merge_money:(BOOL)merge_money{
    _history_ok_num = history_ok_num;
    _history_store_num = history_store_num;
    _history_money = history_money;
    _today_ok_num = today_ok_num;
    _today_store_num = today_store_num;
    _today_money = today_money;
    _merge_money = merge_money;
    
    _itemsArray = @[@"当日历史已铺店铺达标数",@"当日历史已铺店铺",@"当日历史已铺店铺达标提成",@"当日已铺店铺达标",@"当日已铺店铺",@"当日已铺店铺达标提成",@"当日提成"];
    
    _typeArray = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:_history_ok_num],
                  [NSNumber numberWithBool:_history_store_num],
                  [NSNumber numberWithBool:_history_money],
                  [NSNumber numberWithBool:_today_ok_num],
                  [NSNumber numberWithBool:_today_store_num],
                  [NSNumber numberWithBool:_today_money],
                  [NSNumber numberWithBool:_merge_money], nil];
}

#pragma mark - 创建选择View

- (void)creatBackButton{
    self.view.backgroundColor = kClearColor;
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height)];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    backButton.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
    [backButton addTarget:self action:@selector(backButtonC:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void)backButtonC:(UIButton *)button{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)creatJumpView{
    if (!_jumpView) {
        _jumpView = [[UIView alloc] initWithFrame:CGRectMake(10, -200, kScreenSize.width - 20, 10)];
        
        UIImageView *singelImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_viewFrame.origin.x - 20 + _viewFrame.size.width/2, 0, 33, 17)];
        singelImageView.image = [UIImage imageNamed:@"topSingel"];
        [_jumpView addSubview:singelImageView];
        
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, singelImageView.allHeight, _jumpView.sizeWidth, _jumpView.sizeHeight - singelImageView.sizeHeight)];
        contentView.backgroundColor = kWhiteColor;
        contentView.layer.masksToBounds = YES;
        contentView.layer.cornerRadius = 10;
        [_jumpView addSubview:contentView];
        
        CGFloat width = 0;
        UIButton *button;
        CGFloat lines = 0;

        for (NSInteger i = 0; i < _itemsArray.count; i ++) {
            NSString *nameStr = [NSString stringWithFormat:@"  %@  ",_itemsArray[i]];
            CGSize titleSize = [nameStr boundingRectWithSize:CGSizeMake(_jumpView.sizeWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
            
            if (width + 10 + titleSize.width + 3 >= _jumpView.sizeWidth) {
                lines ++;
                width = 0;
            }
            
            button = [[UIButton alloc] initWithFrame:CGRectMake(15 + width, 10 + (10 + 30) *lines,titleSize.width, 30)];
            width += 10 + titleSize.width;
            button.layer.masksToBounds = YES;
            button.layer.borderWidth = 1;
            button.layer.cornerRadius = 15;
            [button setTitle:_itemsArray[i] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.tag = 1000 + i;
            
            NSNumber *typeValue = _typeArray[i];
            
            if (typeValue.integerValue > 0) {
                button.backgroundColor = _colorsArray[i];
                button.layer.borderColor = [kWhiteColor CGColor];
                [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            }else{
                button.backgroundColor = kWhiteColor;
                button.layer.borderColor = [_colorsArray[i] CGColor];
                [button setTitleColor:_colorsArray[i] forState:UIControlStateNormal];
            }
            

            
            [button addTarget:self action:@selector(chooseLabelsButton:) forControlEvents:UIControlEventTouchUpInside];
            
            [contentView addSubview:button];
            contentView.frame = CGRectMake(contentView.orginX, contentView.orginY, contentView.sizeWidth , button.allHeight + 20);
            
        }
        _jumpView.frame = CGRectMake(_jumpView.orginX, _jumpView.orginY, _jumpView.sizeWidth, contentView.allHeight);
        [self.view addSubview:_jumpView];
    }
}

#pragma mark - 点击选择

- (void)chooseLabelsButton:(UIButton *)button{
    NSInteger tag = button.tag - 1000;
    NSNumber *typeValue = _typeArray[tag];
    if (typeValue.integerValue > 0) {//选中状态 改为未选中
        button.backgroundColor = kWhiteColor;
        button.layer.borderColor = [_colorsArray[tag] CGColor];
        [button setTitleColor:_colorsArray[tag] forState:UIControlStateNormal];
        
        [_typeArray replaceObjectAtIndex:tag withObject:[NSNumber numberWithBool:NO]];
//        _typeArray[tag] =[NSNumber numberWithBool:NO];

    }else{
        button.backgroundColor = _colorsArray[tag];
        button.layer.borderColor = [kWhiteColor CGColor];
        [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
        
        [_typeArray replaceObjectAtIndex:tag withObject:[NSNumber numberWithBool:YES]];
    }
    
    if (_type.integerValue == 3) {//提成
        
        [_delegate showCommistionHistoryInfoWithHistory_ok_num:_typeArray[0] history_store_num:_typeArray[1] history_money:_typeArray[2] today_ok_num:_typeArray[3] today_store_num:_typeArray[4] today_money:_typeArray[5] merge_money:_typeArray[6]];
        
    }else if (_type.integerValue == 1){//历史
        [_delegate showHistoryPersonInfoWithCharging_average_per_day_add:_typeArray[0] store_average_per_day_add:_typeArray[1] store_num:_typeArray[2] charging_num:_typeArray[3] sub_count:_typeArray[4] use_charging_num:_typeArray[5] ok_store_num:_typeArray[6] charging_rate:_typeArray[7] wifi_num:_typeArray[8] wifi_sub_count:_typeArray[9] wifi_used_num:_typeArray[10] mima_num:_typeArray[11] mima_sub_count:_typeArray[12] mima_used_num:_typeArray[13]];
    }
    else{//单店
        
        [_delegate showSingleShopInfoWithGrowth_fans_num:_typeArray[0] total_charging_num:_typeArray[1] used_charging_num:_typeArray[2] average_per_day_add:_typeArray[3] charging_rate:_typeArray[4] dabiao:_typeArray[5]];
    }
    
}

//初始化数组

- (void)loadArrays{
    _colorsArray = @[kColorFrom0x(0x5fa1c4),kColorFrom0x(0x5f79c4),kColorFrom0x(0xc45f5f),kColorFrom0x(0xb25fc4),kColorFrom0x(0x9dc45f),kColorFrom0x(0x5fc487),kColorFrom0x(0xc4b05f),kColorFrom0x(0xbfd3b7),
                     kColorFrom0x(0x3cb371),
                     kColorFrom0x(0xcd5c5c),
                     kColorFrom0x(0x66ffff),
                     kColorFrom0x(0x6600ff),
                     kColorFrom0x(0x660099),
                     kColorFrom0x(0xFFB6C1)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
